﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Friends : System.Web.UI.Page
{
    public static int currentFriendElement;
    public static int currentPossibleFriendElement;
    public static int currentFriendDirElement;

    private static int previousFriendCount;
    private static int previousPossibleFriendCount;
    private static int previousFriendDirCount;

    private string firstname;
    private string lastname;
    private string email;
    private string username;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            ResetOnPostBack();
        }
        else
        {
            GetCurrentUserInfo();
            currentFriendElement = ++lbxFriends.SelectedIndex;
            currentPossibleFriendElement = ++lbxPossibleFriends.SelectedIndex;
            currentFriendDirElement = ++lbxFriendDir.SelectedIndex;
            DoLoadFriendList();
            DoLoadPossibleFriendList();
            DoLoadFriendDirectory();
        }

        previousFriendCount = lbxFriends.Items.Count;
        previousPossibleFriendCount = lbxPossibleFriends.Items.Count;
        previousFriendDirCount = lbxFriendDir.Items.Count;

    }

    private void ResetOnPostBack()
    {
        GetCurrentUserInfo();

        // listbox must contain value -1 if empty, else use the current element (initialized as 0 by default)
        lbxFriendDir.SelectedIndex = lbxFriendDir.SelectedIndex < 0 ? -1 : currentFriendDirElement;
        lbxFriends.SelectedIndex = lbxFriends.SelectedIndex < 0 ? -1 : currentFriendElement;
        lbxPossibleFriends.SelectedIndex = lbxPossibleFriends.SelectedIndex < 0 ? -1 : currentPossibleFriendElement;
    }

    private void GetCurrentUserInfo()
    {
        firstname = Session["Firstname"].ToString();
        lastname = Session["Lastname"].ToString();
        email = Session["EmailID"].ToString();
        username = Session["Username"].ToString();
    }

    private void DoLoadFriendList()
    {
        Friend.WebDirectory = Server.MapPath("App_data");
        Friend friend = new Friend(firstname, lastname, username, email);
        LoadFriendList(friend);
    }

    private void DoLoadPossibleFriendList()
    {
        Friend.WebDirectory = Server.MapPath("App_Data");

        try
        {
            Friend friend = new Friend(firstname, lastname, username, email);
            LoadPossibleFriendList(friend);
        }
        catch (System.ArgumentOutOfRangeException) { } // if caught, just continue.  it means there aren't any possible friends at this time.
        catch (Exception ex) { }
    }

    private void DoLoadFriendDirectory()
    {
        this.LoadFriendDirectory();
    }

    protected void lbxFriends_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowFriendInfo();
    }

    private void ShowFriendInfo()
    {
        try
        {
            Friend friend = new Friend();
            friend = friend.SplitFriendTextToObject(lbxFriends.SelectedItem.Value);

            lblName.Text = string.Format("Name: {0} {1}", friend.FirstName, friend.LastName);
            lblEmail.Text = string.Format("Email: {0}", friend.Email);
            lblId.Text = string.Format("UserName: {0}", friend.UserName);
        }
        catch (Exception ex) { }
    }

    private void LoadFriendList(Friend friend)
    {
        if (!friend.hasFriendListFile())
        {
            friend.CreateFriendListFile();
        }
        else
        {
            friend.FriendList = friend.GetFriendListFromFile();
        }

        PopulateListBox(friend);
    }

    private void PopulateListBox(Friend friend)
    {
        foreach (Friend aFriend in friend.FriendList)
            lbxFriends.Items.Add(aFriend.ToString());
    }


    protected void LoadPossibleFriendList(Friend friend)
    {
        if (friend.hasFriendListFile())
        {
            friend.FriendList = friend.GetFriendListFromFile();

            foreach (Friend aFriend in friend.FriendList)
            {
                if (aFriend.hasFriendListFile())
                {
                    aFriend.FriendList = aFriend.GetFriendListFromFile();
                    foreach (Friend friendFriend in aFriend.FriendList)
                        lbxPossibleFriends.Items.Add(friendFriend.ToString());
                }
            }
        }

    }

    protected void btnAddFriend_Click(object sender, EventArgs e)
    {
        String item = lbxFriends.SelectedValue;
        Friend friend = new Friend(firstname, lastname, username, email);

        if (friend.hasFriendListFile())
        {
            Friend newFriend = friend.SplitFriendTextToObject(item);
            friend.AddFriendEntryToFile(newFriend);
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }

    protected void btnUp_Click(object sender, EventArgs e)
    {
        try
        {
            currentFriendElement = lbxFriends.SelectedIndex;
            if (currentFriendElement > -1)
            {
                currentFriendElement--;
                lbxFriends.SelectedIndex = currentFriendElement;
                ShowFriendInfo();
            }
            else
            {
                currentFriendElement = 0;
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnDown_Click(object sender, EventArgs e)
    {
        try
        {
            currentFriendElement = lbxFriends.SelectedIndex;
            if (lbxFriends.Items.Count - 1 != currentFriendElement)
            {
                currentFriendElement++;
                lbxFriends.SelectedIndex = currentFriendElement;
                ShowFriendInfo();
            }
            else
            {
                currentFriendElement = 0;
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnUp2_Click(object sender, EventArgs e)
    {
        try
        {
            if (currentPossibleFriendElement > -1)
            {
                currentPossibleFriendElement--;
                lbxPossibleFriends.SelectedIndex = currentPossibleFriendElement;
                ShowPossibleFriendInfo();
            }
            else
            {
                currentPossibleFriendElement = 0;
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnDown2_Click(object sender, EventArgs e)
    {
        try
        {
            if ((lbxPossibleFriends.Items.Count - 1 != currentPossibleFriendElement)
                && (currentPossibleFriendElement > -1))
            {
                currentPossibleFriendElement++;
                lbxPossibleFriends.SelectedIndex = currentPossibleFriendElement;
                ShowPossibleFriendInfo();
            }
            else
            {
                currentPossibleFriendElement = 0;
            }
            
        }
        catch (Exception ex)
        {
        }
    }
    protected void lbxPossibleFriends_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowPossibleFriendInfo();
    }

    private void ShowPossibleFriendInfo()
    {
        try
        {
            int index = lbxPossibleFriends.SelectedIndex;
            Friend friend = new Friend();
            friend = friend.SplitFriendTextToObject(lbxPossibleFriends.SelectedItem.Value);

            lblNamepf.Text = string.Format("Name: {0} {1}", friend.FirstName, friend.LastName);
            lblEmailpf.Text = string.Format("Email: {0}", friend.Email);
            lblIdpf.Text = string.Format("ID: {0}", friend.UserName);
        }
        catch (Exception ex) { }
    }


    protected void btnAddPossibleFriend_Click(object sender, EventArgs e)
    {
        String item = lbxFriends.SelectedValue;
        Friend friend = new Friend(firstname, lastname, username, email);

        if (friend.hasFriendListFile())
        {
            Friend newFriend = friend.SplitFriendTextToObject(item);
            friend.AddFriendEntryToFile(newFriend);
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }

    protected void lbxFriendDir_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowFriendDirInfo();
    }

    protected void LoadFriendDirectory()
    {
        Friend friendDirectory = new Friend();
        friendDirectory.FriendList = friendDirectory.GetAllFriends();

        foreach (Friend friend in friendDirectory.FriendList)
        {
            lbxFriendDir.Items.Add(friend.ToString());
        }
    }



    protected void btnUpDir_Click(object sender, EventArgs e)
    {
        try
        {
            if (currentFriendDirElement > -1)
            {
                currentFriendDirElement--;
                lbxFriendDir.SelectedIndex = currentFriendDirElement;
                ShowFriendDirInfo();
            }
            else
            {
                currentFriendDirElement = 0;
            }

        }
        catch (Exception ex)
        {
        }
    }
    protected void btnDownDir_Click(object sender, EventArgs e)
    {
        try
        {
            if ((lbxFriendDir.Items.Count - 1 != currentFriendDirElement)
                && (currentFriendDirElement > -1))
            {
                currentFriendDirElement++;
                lbxFriendDir.SelectedIndex = currentFriendDirElement;
                ShowFriendDirInfo();
            }
            else
            {
                currentFriendDirElement = 0;
            }

        }
        catch (Exception ex)
        {
        }
    }

    private void ShowFriendDirInfo()
    {
        try
        {
            Friend friend = new Friend();
            friend = friend.SplitFriendTextToObject(lbxFriendDir.SelectedItem.Value);

            lblNameDir.Text = string.Format("Name: {0} {1}", friend.FirstName, friend.LastName);
            lblEmailDir.Text = string.Format("Email: {0}", friend.Email);
            lblIdDir.Text = string.Format("ID: {0}", friend.UserName);
        }
        catch (Exception ex) { }
    }

    protected void btnAddFriendDir_Click(object sender, EventArgs e)
    {
        String item = lbxFriendDir.SelectedValue;
        Friend friend = new Friend(firstname, lastname, username, email);

        if (friend.hasFriendListFile())
        {
            Friend newFriend = friend.SplitFriendTextToObject(item);
            friend.AddFriendEntryToFile(newFriend);
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }

}